package com.shixulin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.ExamPaper;
import com.shixulin.entity.PaperQuestion;
import com.shixulin.mapper.PaperQuestionMapper;
import com.shixulin.service.PaperQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * (PaperQuestion)表服务实现类
 *
 * @author makejava
 * @since 2023-11-20 19:06:15
 */
@Service("paperQuestionService")
//@Transactional(rollbackFor = Exception.class)
@Transactional
public class PaperQuestionServiceImpl extends ServiceImpl<PaperQuestionMapper, PaperQuestion> implements PaperQuestionService {
    @Autowired
    private PaperQuestionMapper paperQuestionMapper;
    @Override
    @Transactional
    public ResponseResult saveQuestions(List<PaperQuestion> paperQuestion) {

        boolean flag = paperQuestionMapper.saveQuestions(paperQuestion);
//          int insert = this.baseMapper.insert(paperQuestion);
        try{
            if (flag) {
                return new  ResponseResult(300,"插入成功");
            }else {
                return new  ResponseResult(507,"插入失败");
            }

        }catch (Exception e) {
            return new  ResponseResult(407,"错误");
        }
    }
    @Override
    public ResponseResult deletePaperId(int paperId) {
        try{
            paperQuestionMapper.deletePaper(paperId);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResponseResult deleteExamQuestion(int questionId) {

        try{
            boolean delete = paperQuestionMapper.deleteExamQuestion(questionId);
            if (delete) {
                return new ResponseResult(300,"删除成功");
            }else{
                return new  ResponseResult(507,"插入失败");
            }
        }catch (Exception e) {
            return new  ResponseResult(407,"错误");
        }

    }
    //删除试卷题目
    @Override
    public boolean deletePaperQuestionById(int paperId) {

            boolean delete = paperQuestionMapper.deletePaperQuestionById(paperId);

            return delete;
    }

    @Override
    public ResponseResult getPaperQuestion(int paperId) {

     try {

         QueryWrapper queryWrapper = new QueryWrapper();

         queryWrapper.eq("paper_id",paperId);

         List<PaperQuestion> paperQuestions = this.baseMapper.selectList(queryWrapper);

         if(paperQuestions!=null&&!paperQuestions.isEmpty()){
             return new ResponseResult(300,"查询成功",paperQuestions);
         }else{
             return new  ResponseResult(507,"失败");
         }
     }catch (Exception e){
         return new  ResponseResult(407,"错误");
     }
    }


}
